<?php

namespace backend\modules\byxcx\controllers;

use Yii;
use common\models\base\SearchModel;
use common\traits\Curd;
use common\enums\StatusEnum;
use backend\controllers\BaseController;
use common\models\byxcx\Chart;


/**
 * 客户端
 *
 * Class ClientController
 * @package backend\modules\member\controllers
 */
class ChartController extends BaseController
{
    use Curd;

    /**
     * @var Client
     */
    public $modelClass = Chart::class;

    /**
     * 首页
     *
     * @return string
     * @throws \yii\web\NotFoundHttpException
     */
    public function actionIndex()
    {
        $searchModel = new SearchModel([
            'model' => $this->modelClass,
            'scenario' => 'default',
            'partialMatchAttributes' => ['title','cate.title'], // 模糊查询
            'defaultOrder' => [
                'id' => SORT_DESC
            ],
            'pageSize' => $this->pageSize
        ]);

        $dataProvider = $searchModel
            ->search(Yii::$app->request->queryParams);
        $dataProvider->query
            ->andWhere(['>=', 'status', StatusEnum::DISABLED]);
        return $this->render($this->action->id, [
            'dataProvider' => $dataProvider,
            'searchModel' => $searchModel,
            'menuDropDownList'=> Yii::$app->services->menu->getDropDowns(),
        ]);
    }

    /**
 * ajax编辑/创建
 *
 * @return mixed|string|\yii\web\Response
 * @throws \yii\base\ExitException
 */
    public function actionAjaxEdit()
    {
        $id = Yii::$app->request->get('id');
        $model = $this->findModel($id);

        $this->activeFormValidate($model);
        if ($model->load(Yii::$app->request->post())) {
            return $model->save()
                ? $this->redirect(Yii::$app->request->referrer)
                : $this->message($this->getError($model), $this->redirect(Yii::$app->request->referrer), 'error');
        }
        return $this->renderAjax($this->action->id, [
            'model' => $model,
            'menuDropDownLists' => Yii::$app->services->menu->getDropDowns(),
        ]);
    }
}